package cn.ibent;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.ibent.common.OsHelper;
import cn.ibent.http.HttpServerInitializer;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.socket.ServerSocketChannel;
import io.netty.channel.socket.SocketChannel;

/**
 * Hello world!
 *
 */
public class BootStrap 
{
    private static Logger logger = LoggerFactory.getLogger(BootStrap.class.getSimpleName());
    public static final Class<? extends ServerSocketChannel> clazzServerSocketChannel = OsHelper.serverSocketChannelClazz();
    public static final Class<? extends SocketChannel> clazzSocketChannel = OsHelper.socketChannelClazz();
    public static void main( String[] args )
    {
        EventLoopGroup bossGroup = OsHelper.buildEventLoopGroup(1);
        EventLoopGroup workerGroup = OsHelper.buildEventLoopGroup(0);
        try {
            ServerBootstrap httpBootStrap = new ServerBootstrap();
            httpBootStrap.group(bossGroup, workerGroup)
                    .channel(clazzServerSocketChannel)
                    .childOption(ChannelOption.AUTO_READ, Boolean.FALSE)
                    .childHandler(new HttpServerInitializer());
            Channel httpServerChannel = httpBootStrap.bind(21058).sync().channel();
            logger.info("server start at 21058 port...");
            httpServerChannel.closeFuture().sync();
        } catch (InterruptedException e) {} finally {
            bossGroup.shutdownGracefully();
            workerGroup.shutdownGracefully();
        }
    }
}
